package com.cxj.magic;

import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.cxj.magic.R;

public class ImgViewer extends Activity {
    int imgid = 2130837505;
    private TouchExampleView teView;
    private LinearLayout layMain;

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.imgviewer);
        Bundle localBundle = getIntent().getExtras();
        if (localBundle != null)
            this.imgid = localBundle.getInt("imgid");
        teView = new TouchExampleView(this, this.imgid);
        teView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        layMain = (LinearLayout) findViewById(R.id.layMain);
        layMain.addView(teView);
        ((Button) findViewById(R.id.btnSetWallmap)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(ImgViewer.this).setTitle(R.string.app_about).setMessage(R.string.app_set_wallmap_msg)
                        .setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                                InputStream is = null;
                                try {
                                    is = ImgViewer.this.getBaseContext().getResources().openRawResource(ImgViewer.this.imgid);
                                    ImgViewer.this.setWallpaper(is);
                                    Toast.makeText(ImgViewer.this, R.string.my_text_pre, 0).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                } finally {
                                    IOUtil.closeQuitely(is);
                                }
                            }
                        }).setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                                Toast.makeText(ImgViewer.this, R.string.my_text_no, 0).show();
                            }
                        }).show();
            }
        });
//        ((Button) findViewById(R.id.btnReturn)).setOnClickListener(new View.OnClickListener() {
//            public void onClick(View paramView) {
//                ImgViewer.this.finish();
//            }
//        });
        ((ImageButton) findViewById(R.id.btnNext)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                if (teView != null) {
                    layMain.removeView(teView);
                }
                ImgViewer.this.imgid = Main.getValidPos(ImgViewer.this.imgid + 1);
                teView = new TouchExampleView(ImgViewer.this, ImgViewer.this.imgid);
                layMain.addView(teView);
            }
        });
        ((ImageButton) findViewById(R.id.btnPriv)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                if (teView != null) {
                    layMain.removeView(teView);
                }
                ImgViewer.this.imgid = Main.getValidPos(ImgViewer.this.imgid - 1);
                teView = new TouchExampleView(ImgViewer.this, ImgViewer.this.imgid);
                layMain.addView(teView);
            }
        });
    }
}